package com.dongway.netcondition.socketToBox;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import com.dongway.netcondition.model.DevInfo;

public class SetDevIsListeren {

    public static void setDevIsListeren(DevInfo devInfo) {

        Integer dev_id = devInfo.getId();
        Integer time = devInfo.getCheckIntervalTime();
        Integer times = devInfo.getCheckTimes();

        Integer num1 = devInfo.getPnServerIpState();
        Integer num2 = devInfo.getPnLocalIpState();
        Integer num3 = devInfo.getVpnServerIpState();
        Integer num4 = devInfo.getVpnLocalIpState();
        Integer num = num1 + num2 * 2 + num3 * 4 + num4 * 8;

        Socket socket = SocketContent.getInstance().getSocket(dev_id);
        byte[] b = new byte[48]; // 发送设置报文指令长度

        String hex = "";
        // 发送指令头
        byte[] b1 =
                { 0x61, 0x31, 0x30, 0x2c, (byte) 0x88, (byte) 0x87, 0x06, 0x06, 0x26, 0x00, 0x00,
                 0x00 };

        // 根据ID设置
        hex = Integer.toHexString(dev_id);
        hex = hex.length() > 2 ? hex : ("0" + hex);
        byte[] b2 = HexStringToBytes.hexStringToBytes(hex);
        b1[10] = b2[0];

        for (int i = 0; i < 12; i++) {
            b[i] = b1[i];
        }

        // 设置心跳时间，单位秒
        hex = Integer.toHexString(time);
        hex = hex.length() > 2 ? hex : ("0" + hex);
        b2 = HexStringToBytes.hexStringToBytes(hex);
        b[12] = b2[0];

        // 连续检测数目
        hex = Integer.toHexString(times);
        hex = hex.length() > 2 ? hex : ("0" + hex);
        b2 = HexStringToBytes.hexStringToBytes(hex);
        b[16] = b2[0];

        // 根据num的二进制来设置是否监听网络
        hex = Integer.toHexString(num); //
        hex = hex.length() > 2 ? hex : ("0" + hex);
        b2 = HexStringToBytes.hexStringToBytes(hex);
        b[36] = b2[0];

        try {
            OutputStream out = socket.getOutputStream();
            out.write(b);
            out.close();
        } catch (IOException e) {
            System.out.println("设置失败！");
        }

    }

}
